<?php
/**
 * Project: AIGC
 * Author: moufer <moufer@163.com>
 * Date: 2024/7/15 14:21
 */

namespace module\user\controller\admin;

use app\library\Controller;
use module\user\front\form\UserForm;
use module\user\logic\admin\UserLogic;
use mof\annotation\AdminPerm;
use mof\annotation\Inject;
use mof\ApiResponse;
use think\response\Json;

#[AdminPerm(
    title: '用户管理', url: 'user/user', actions: '*',
    sort: 2, icon: 'User', group: 'main'
)]
class User extends Controller
{
    #[Inject]
    protected UserLogic $logic;

    #[Inject]
    protected UserForm $form;

    public function index(): Json
    {
        return ApiResponse::success(
            $this->logic->paginate($this->request->searcher())
        );
    }

    public function edit($id): Json
    {
        return ApiResponse::success($this->form->build($this->logic->read($id)));
    }

    public function update($id): Json
    {
        $model = $this->logic->update(
            $id, $this->form->withScene('edit')->withFixed(['id' => $id])->get()
        );
        return ApiResponse::success($model);
    }

    public function updates($id, $field, $value): Json
    {
        $this->logic->updates($id, $field, $value);
        return ApiResponse::success();
    }

    public function delete($id): Json
    {
        $this->logic->delete($id);
        return ApiResponse::success();
    }

    public function deletes($id): Json
    {
        $this->logic->deletes($id);
        return ApiResponse::success();
    }

}